/*
 * File: gov.kdrm.mygset.action.common.ReferenceActionBean.java
 *
 * Copyright (c) 2009 PT. XYBASE INDONESIA,
 * All Rights Reserved.
 *
 * This software is the confidential and proprietary information
 * of PT. XYBASE INDONESIA ("Confidential Information"). You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered
 * into with PT. XYBASE INDONESIA.
 *
 * Project:	mygset2
 * Created: Jul 06, 2009 2:00:00 PM
 */
package app.rci.util;

import java.util.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * <p>This class represent a helper for building JSON string for Combo</p>
 *
 * <p>Revision Information:<br/><i>
 * $Date: 2010-04-16 08:19:05 +0700 (Fri, 16 Apr 2010) $<br/>
 * $Revision: 26321 $
 * $LastChangedBy: sigit.surendra $
 * </i></p>
 *
 * @author Imam
 */
public class ComboUtil<T> extends JSONUtil {
    private static final String DEFAULT_LABEL = "ALL";
    private String id;
    private String label;
    private String defaultLabel;
    private String title;
    private Collection<T> list;
    private Class<?> clazz;

    public ComboUtil(Class<?> clazz, Collection<T> list, String id, String label) {
        this(clazz, list, id, label, DEFAULT_LABEL);
    }

    public ComboUtil(Class<?> clazz, Collection<T> list, String id, String label, String defaultLabel) {
        super();
        // set class reference
        this.clazz = clazz;
        // set property name for id
        this.id = id;
        // set property name for label
        this.label = label;
        // set default label
        this.defaultLabel = defaultLabel;
        // set collection
        this.list = list;
    }

    public ComboUtil(Class<?> clazz, Collection<T> list, String id, String label, String defaultLabel, String title) {
        super();
        // set class reference
        this.clazz = clazz;
        // set property name for id
        this.id = id;
        // set property name for label
        this.label = label;
        // set default label
        this.defaultLabel = defaultLabel;
        // set collection
        this.list = list;

        this.title = title;
    }

    @Override
    public String serialize() {
        List<Map<String, Object>> options = new ArrayList<Map<String, Object>>();
        // add default label on first option
        Map<String, Object> defaultOption = new HashMap<String, Object>(2);
        defaultOption.put("id", "");
        defaultOption.put("label", getDefaultLabel());
        defaultOption.put("title", "");
        options.add(defaultOption);
        // loop through the colletion
        for (T obj : list) {
            Map<String, Object> option = new HashMap<String, Object>(2);
            try {
                // get id based on property passed on argument
                Method getId = clazz.getMethod(StringUtils.toGetterName(id));
                option.put("id", getId.invoke(obj));

                // get label based on property passed on argument
                Method getLabel = clazz.getMethod(StringUtils.toGetterName(label));
                option.put("label", getLabel.invoke(obj));

                // get label based on property passed on argument
                Method getTitle = clazz.getMethod(StringUtils.toGetterName(title));
                option.put("title", getTitle.invoke(obj));

                // put into list
                options.add(option);
            } catch (NoSuchMethodException ignored) {
            } catch (InvocationTargetException ignored) {
            } catch (IllegalAccessException ignored) { }
        }
        // put list into object options
        addData("options", options);
        return super.serialize();
    }

    public String getDefaultLabel() {
        return defaultLabel == null ? DEFAULT_LABEL : defaultLabel;
    }

    public void setDefaultLabel(String defaultLabel) {
        this.defaultLabel = defaultLabel;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public Collection<T> getList() {
        return list;
    }

    public void setList(Collection<T> list) {
        this.list = list;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}
